﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace WebAddressBook
{
    public partial class MangeNotices : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDLEnterprise();
                InitDDLDept();
                InitDDLEnterpriseForSearch();
                InitDDLDeptForSearch();
                //InitGVNotice("");
            }
        }

        protected void DDLEnterprise_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDDLDept();
        }

        protected void BtSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DDLEnterprise.SelectedValue))
            {
                LbMessage.Text = "<div class='box box-error'>没有企业，请先创建企业架构！</div>";
                return;
            }
            if (!VerifyIupt())
            {
                LbMessage.Text = "<div class='box box-error'>带 * 号字段不能为空</div>";
                return;
            }

            Adrr.Model.Notices noticesModel = new Adrr.Model.Notices();
            Adrr.BLL.Notices noticesBLL = new Adrr.BLL.Notices();
            if (string.IsNullOrEmpty(DDLDept.SelectedValue))
                noticesModel.PublishDeptID = DDLEnterprise.SelectedValue;
            else
                noticesModel.PublishDeptID = DDLDept.SelectedValue;
            noticesModel.Title = TBTitle.Text.Trim();
            noticesModel.Contents = TBContent.Text.Trim();
            noticesModel.CreateTime = DateTime.Now;
            noticesModel.PublishTime = DateTime.Now;
            //TODO:
            /*
             * 这里要改成session["userid"];
             */
            noticesModel.PublishUserID = "admin";

            if (noticesBLL.Add(noticesModel))
                LbMessage.Text = "<div class='box box-info'>添加通知公告成功！</div>";
            else
                LbMessage.Text = "<div class='box box-error'>添加通知公告失败！</div>";
        }

        private bool VerifyIupt()
        {
            if (string.IsNullOrEmpty(TBTitle.Text.Trim()) || string.IsNullOrEmpty(TBContent.Text.Trim()))
                return false;
            else
                return true;
        }

        private void InitDDLEnterprise()
        {
            DDLEnterprise.DataSource = CommonHelper.GetEnterprise();
            DDLEnterprise.DataTextField = "DeptName";
            DDLEnterprise.DataValueField = "DeptID";
            DDLEnterprise.DataBind();
        }

        private void InitDDLDept()
        {
            string enterpriseId = DDLEnterprise.SelectedValue;
            DDLDept.DataSource = CommonHelper.GetDeptList(enterpriseId);
            DDLDept.DataTextField = "DeptName";
            DDLDept.DataValueField = "DeptID";
            DDLDept.DataBind();

            DDLDept.Items.Add(new ListItem("所有部门", ""));
            DDLDept.SelectedValue = "";
        }

        private void InitGVNotice(string strWhere)
        {
            Adrr.BLL.Notices noticesBLL = new Adrr.BLL.Notices();

            GVNotices.DataSource = noticesBLL.GetListunion(strWhere);
            GVNotices.DataKeyNames = new string[] { "NoticeID" };
            GVNotices.DataBind();
        }

        private void InitDDLEnterpriseForSearch()
        {
            DataSet ds = CommonHelper.GetEnterprise();
            DDLEnterpriseForSearch.DataSource = ds;
            DDLEnterpriseForSearch.DataTextField = "DeptName";
            DDLEnterpriseForSearch.DataValueField = "DeptID";
            DDLEnterpriseForSearch.DataBind();
        }

        private void InitDDLDeptForSearch()
        {
            string enterpriseId = DDLEnterpriseForSearch.SelectedValue;
            DataTable dt = CommonHelper.GetDeptList(enterpriseId);
            DDLDeptmentForSearch.DataSource = dt;
            DDLDeptmentForSearch.DataTextField = "DeptName";
            DDLDeptmentForSearch.DataValueField = "DeptID";
            DDLDeptmentForSearch.DataBind();

            DDLDeptmentForSearch.Items.Add(new ListItem("全部", ""));
            DDLDeptmentForSearch.SelectedValue = "";
        }

        protected void DDLEnterpriseForSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDDLDeptForSearch();
        }

        protected void BtSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DDLEnterpriseForSearch.SelectedValue))
                return;

            Search();
        }

        private void Search()
        {
            StringBuilder strWhere = new StringBuilder();
            if (!string.IsNullOrEmpty(DDLDeptmentForSearch.SelectedValue) && !string.IsNullOrEmpty(content.Value.Trim()))
            {
                strWhere.Append("PublishDeptID like '" + DDLDeptmentForSearch.SelectedValue + "%'");
                strWhere.Append(" and Contents like '%" + content.Value.Trim() + "%'");
            }
            else
            {
                if (!string.IsNullOrEmpty(DDLDeptmentForSearch.SelectedValue))
                    strWhere.Append("PublishDeptID like '" + DDLDeptmentForSearch.SelectedValue + "%'");
                else
                    strWhere.Append("PublishDeptID like '" + DDLEnterpriseForSearch.SelectedValue + "%'");
                if (!string.IsNullOrEmpty(content.Value.Trim()))
                    strWhere.Append(" and Contents like '%" + content.Value.Trim() + "%'");
            }
            InitGVNotice(strWhere.ToString());
        }

        protected void GVNotices_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除该通知公告：\"" + e.Row.Cells[0].Text.ToUpper() + "\" 吗?')");
                }
            }
        }

        protected void GVNotices_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string noticeId = GVNotices.DataKeys[e.RowIndex].Value.ToString();
            Adrr.BLL.Notices noticeBLL = new Adrr.BLL.Notices();
            bool flag = noticeBLL.Delete(new Guid(noticeId));
            if (flag)
                Search();
        }
    }
}